<?php 
namespace app\common\model;
class Favorites extends Common{
	protected $insert = ['addtime'=>SYS_TIME];
	public function getAddtimeAttr($value, $data){
		return date('Y-m-d H:i', $value);
	}
	public function save_datas($data){
		$ids = $this->where('contentid', $data['contentid'])->where('typeid', $data['typeid'])->where('userid', $data['userid'])->delete();
		$model = $data['typeid'] == 1 ? model('Member') : model('Requires');
		if ($ids) {
			$model->where('id',$data['contentid'])->setDec('favs');
			return $model->where('id', $data['contentid'])->value('favs');
		}else{
			$this->save_data($data);
			$model->where('id',$data['contentid'])->setInc('favs');
			return $model->where('id', $data['contentid'])->value('favs');
		}
	}
	public function getUsernameAttr($value, $data){
		$username = model('Member')->where('id',$data['userid'])->value('username');
		return $username;
	}
	public function getContentAttr($value, $data){
		if($data['typeid'] == 1){
			$model = 'Member';
			$content = model($model)->where('id',$data['contentid'])->value('username');
		}else{
			$model = 'Requires';
			$content = model($model)->where('id',$data['contentid'])->value('title');
		}
		return $content;
	}
	public function getTypeidAttr($value, $data){
		return $value == 1 ? '关注用户' : '收藏需求' ;
	}
	/**
	 * 给关注该用户的人发送下单消息
	 * @param  int  $userid   下单人id
	 * @param  string  $nickname 下单人昵称
	 * @param  string  $title    下单标题
	 * @param  int     $typeid   类型
	 * @return int    
	 */
	public function send_sysmsg($userid, $nickname, $requiresid, $title, $typeid=1){
		$list = $this->list_data(['typeid'=>1, 'contentid'=>$userid]);
		$info = [];
		foreach ($list as $v) {
			$info[] = $v['userid'];
		}
		send_rongyun_sys_msg($info,$nickname.'发布新订单--‘'.$title.'’，有兴趣的可以前往预约。', $requiresid, 1);
		return true;
	}
}